package com.huawei.updatesdk.a.a.c.b;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.huawei.updatesdk.a.a.b.a.a.a;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;

public class b {
    private static String a = "NetworkUtil";
    private static int b = -1;
    private static int c = -1;
    private static Proxy d = null;

    private static int a(NetworkInfo networkInfo) {
        if (networkInfo == null || !networkInfo.isConnected()) {
            return 0;
        }
        int type = networkInfo.getType();
        if (1 == type || 13 == type) {
            return 1;
        }
        if (type != 0) {
            return 0;
        }
        switch (networkInfo.getSubtype()) {
            case 1:
            case 2:
            case 4:
            case 7:
            case 11:
                return 2;
            case 3:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
            case 12:
            case 14:
            case 15:
                return 3;
            case 13:
                return 4;
            default:
                return 0;
        }
    }

    public static Proxy a() {
        return d;
    }

    private static void a(int i) {
        c = i;
    }

    private static void a(Proxy proxy) {
        d = proxy;
    }

    public static boolean a(Context context) {
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager != null) {
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                    return true;
                }
            }
        }
        return false;
    }

    private static boolean a(NetworkInfo networkInfo, Context context) {
        int type = networkInfo.getType();
        boolean b = a.b(context);
        if (type != 0 || !b) {
            return false;
        }
        String host = android.net.Proxy.getHost(context);
        int port = android.net.Proxy.getPort(context);
        Proxy proxy = null;
        if (!(host == null || host.length() <= 0 || port == -1)) {
            proxy = new Proxy(Type.HTTP, new InetSocketAddress(host, port));
        }
        a(proxy);
        return true;
    }

    public static boolean b(Context context) {
        if (-1 == c) {
            c(context);
        }
        return c == -3;
    }

    public static int c(Context context) {
        if (-1 == b) {
            a.a(a, "getPsType() need init");
            if (context != null) {
                e(context);
            }
        }
        return b;
    }

    public static Proxy d(Context context) {
        return b(context) ? a() : null;
    }

    private static void e(Context context) {
        a(0);
        NetworkInfo f = f(context);
        if (f == null) {
            a.a(a, "setPsType() info = null");
            return;
        }
        b = a(f);
        if (1 == b) {
            return;
        }
        if (a(f, context)) {
            a(-3);
        } else {
            a(-2);
        }
    }

    private static NetworkInfo f(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        return connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
    }
}
